﻿using System;
using System.Runtime.Serialization;

namespace Infrastructure.Logging
{
    /// <summary>
    /// Response object for generic data.
    /// <div>In additional for response data it holds generic type additional data</div>
    /// </summary>
    /// <typeparam name="T">the type of the additional data that is hold by the response object.</typeparam>
    [DataContract(Namespace = "http://Schemas.com/Infrustructure/Contracts/V1")]
    public class GenericResponse<T> : BaseResponse
    {
        /// <summary>
        /// Additional response data.
        /// </summary>
        [DataMember(IsRequired = false)]
        public T Data { get; set; }

        /// <summary>
        /// Init successful response object. 
        /// <div><see cref="BaseResponse.ErrorCode"/> = 0</div>
        /// <div><see cref="BaseResponse.ErrorDescription"/> = string.Empty.</div>
        /// </summary>
        public GenericResponse()
        {}

        /// <summary>
        /// Init successful response object with the given data object.
        /// <div><see cref="BaseResponse.ErrorCode"/> = 0</div>
        /// <div><see cref="BaseResponse.ErrorDescription"/> = string.Empty.</div>
        /// </summary>
        /// <param name="data">the data to init the response object with</param>
        public GenericResponse( T data )
        {
            Data = data;
        }

        /// <summary>
        /// Init response object with the given error code and description.
        /// </summary>
        /// <param name="errorCode">the error code of the response</param>
        /// <param name="errorDescription">the description of the error (not null)</param>
        /// <exception cref="ArgumentNullException"><paramref name="errorDescription"/> is null</exception>
        public GenericResponse( int errorCode, string errorDescription ) : base( errorCode, errorDescription )
        {}

        /// <summary>
        /// Init response object with the given error code and description.
        /// </summary>
        /// <param name="errorCode">the error code of the response</param>
        /// <param name="errorDescription">the description of the error (not null)</param>
        /// <param name="data">the data to init the response object with</param>
        /// <exception cref="ArgumentNullException"><paramref name="errorDescription"/> is null</exception>
        public GenericResponse(int errorCode, string errorDescription, T data )
            : base(errorCode, errorDescription)
        {
            Data = data;            
        }

        public override string ToString()
        {
            return string.Format( "Code: {0}, Description: {1}, Data: {2}", ErrorCode, ErrorDescription, Data );
        }
    }
}
